package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

//实现用户注册功能
//程序启动后要求用户输入:用户名,密码,昵称,年龄
//用户名,密码,昵称不能为空
public class RegDemo {
    public static void main(String[] args) throws Exception {
        UserInfo userinfo = InputUtil.getInputObject(new UserInfo(), "苨冲q币马","注册");
        System.out.println(userinfo);

        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                "root",
                "root"
        );
        System.out.println("成功连接数据库!");
        //3
        //用于执行SQL语句。该对象通过与数据库的连接（connection）进行创建
        Statement statement = connection.createStatement();
        String sql= "INSERT INTO userinfo(username,password,nickname,age) " +
                "VALUES("+
                 "'"+userinfo.getUsername()+"',"+
                "'"+userinfo.getPassword()+"',"+
                "'"+userinfo.getNickname()+"',"+
                userinfo.getAge()+
                ")";
        System.out.println(sql);
        //用于执行一个SQL更新语句
        int num =statement.executeUpdate(sql);
        //
        if (num>0){
            System.out.println("插入成功!");
        }
        connection.close();
    }
}
